<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <el-form :inline="true" :model="queryParams" class="demo-form-inline" @submit.native.prevent>
      <el-form-item label="员工姓名">
        <el-input v-model="queryParams.employeeName" placeholder="请输入员工姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="合同编号">
        <el-input v-model="queryParams.contractNo" placeholder="请输入合同编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="合同类型">
        <el-select v-model="queryParams.contractType" placeholder="请选择合同类型" clearable>
          <el-option label="固定期限" :value="1" />
          <el-option label="无固定期限" :value="2" />
          <el-option label="实习" :value="3" />
        </el-select>
      </el-form-item>
      <el-form-item label="合同状态">
        <el-select v-model="queryParams.status" placeholder="请选择合同状态" clearable>
          <el-option label="生效中" :value="1" />
          <el-option label="已到期" :value="2" />
          <el-option label="已终止" :value="3" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作按钮区域 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['hr:employee:contract:create']">新增</el-button>
      </el-col>
    </el-row>

    <!-- 数据表格 -->
    <el-table v-loading="loading" :data="contractList">
      <el-table-column label="合同编号" align="center" prop="contractNo" />
      <el-table-column label="员工姓名" align="center" prop="employeeName" />
      <el-table-column label="合同类型" align="center" prop="contractType">
        <template slot-scope="scope">
          {{ scope.row.contractType === 1 ? '固定期限' : scope.row.contractType === 2 ? '无固定期限' : '实习' }}
        </template>
      </el-table-column>
      <el-table-column label="开始日期" align="center" prop="startDate" width="100" />
      <el-table-column label="结束日期" align="center" prop="endDate" width="100">
        <template slot-scope="scope">
          {{ scope.row.endDate || '无固定期限' }}
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status === 1 ? 'success' : scope.row.status === 2 ? 'info' : 'danger'">
            {{ scope.row.status === 1 ? '生效中' : scope.row.status === 2 ? '已到期' : '已终止' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)" v-hasPermi="['hr:employee:contract:info']">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['hr:employee:contract:update']">编辑</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['hr:employee:contract:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

    <!-- 添加或修改合同对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="员工" prop="employeeId">
          <el-select v-model="form.employeeId" placeholder="请选择员工" filterable>
            <el-option
              v-for="item in employeeOptions"
              :key="item.id"
              :label="item.name + '（' + item.employeeNo + '）'"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="合同编号" prop="contractNo">
          <el-input v-model="form.contractNo" placeholder="请输入合同编号" />
        </el-form-item>
        <el-form-item label="合同类型" prop="contractType">
          <el-select v-model="form.contractType" placeholder="请选择合同类型">
            <el-option label="固定期限" :value="1" />
            <el-option label="无固定期限" :value="2" />
            <el-option label="实习" :value="3" />
          </el-select>
        </el-form-item>
        <el-form-item label="开始日期" prop="startDate">
          <el-date-picker
            v-model="form.startDate"
            type="date"
            placeholder="选择开始日期"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item label="结束日期" prop="endDate" v-if="form.contractType !== 2">
          <el-date-picker
            v-model="form.endDate"
            type="date"
            placeholder="选择结束日期"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item label="合同附件" prop="attachmentPath">
          <el-upload
            class="upload-demo"
            :action="uploadFileUrl"
            :headers="headers"
            :on-success="handleUploadSuccess"
            :file-list="fileList"
            :limit="1"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传pdf/doc/docx文件，且不超过10MB</div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listContract, getContract, delContract, addContract, updateContract } from '@/api/hr/contract'
import { listEmployee } from '@/api/hr/employee'
import { getToken } from '@/utils/auth'

export default {
  name: 'Contract',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 合同表格数据
      contractList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 员工选项
      employeeOptions: [],
      // 查询参数
      queryParams: {
        page: 1,
        size: 10,
        employeeName: undefined,
        contractNo: undefined,
        contractType: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        employeeId: [
          { required: true, message: '员工不能为空', trigger: 'change' }
        ],
        contractNo: [
          { required: true, message: '合同编号不能为空', trigger: 'blur' }
        ],
        contractType: [
          { required: true, message: '合同类型不能为空', trigger: 'change' }
        ],
        startDate: [
          { required: true, message: '开始日期不能为空', trigger: 'change' }
        ]
      },
      // 上传文件地址
      uploadFileUrl: process.env.VUE_APP_BASE_API + '/hr/employee/contract/upload',
      // 上传文件请求头
      headers: { Authorization: 'Bearer ' + getToken() },
      // 上传文件列表
      fileList: []
    }
  },
  created() {
    this.getList()
    this.getEmployeeOptions()
  },
  methods: {
    /** 查询合同列表 */
    getList() {
      this.loading = true
      listContract(this.queryParams).then(response => {
        this.contractList = response.data.records
        this.total = response.data.total
        this.loading = false
      })
    },
    /** 查询员工下拉选项 */
    getEmployeeOptions() {
      listEmployee().then(response => {
        this.employeeOptions = response.data.records
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加合同'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getContract(id).then(response => {
        this.form = response.data
        if (this.form.attachmentPath) {
          this.fileList = [{ name: '合同附件', url: this.form.attachmentPath }]
        }
        this.open = true
        this.title = '修改合同'
      })
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.$router.push(`/hr/contract/detail/${row.id}`)
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除合同编号为"' + row.contractNo + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return delContract(row.id)
      }).then(() => {
        this.getList()
        this.$message.success('删除成功')
      }).catch(() => {})
    },
    /** 上传成功处理 */
    handleUploadSuccess(response, file, fileList) {
      this.form.attachmentPath = response.data
      this.$message.success('上传成功')
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        employeeId: undefined,
        contractNo: undefined,
        contractType: 1,
        startDate: undefined,
        endDate: undefined,
        status: 1,
        attachmentPath: undefined
      }
      this.fileList = []
      this.resetForm('form')
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id) {
            updateContract(this.form).then(response => {
              this.$message.success('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addContract(this.form).then(response => {
              this.$message.success('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 取消按钮 */
    cancel() {
      this.open = false
      this.reset()
    }
  }
}
</script> 